/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.registration;

import components.TabPanel;
import components.utils.ComponentFactory;
import components.utils.CustomVerifier;
import control.Application;
import entity.DatabaseTest;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class PTestRegistration extends TabPanel {

    /**
     * Creates new form PTestRegistration
     */
    public PTestRegistration() {
        initComponents();
        ComponentFactory.attachVerifier("Por favor ingrese el nombre del análisis.", TFName);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        TFName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        SPrice = new javax.swing.JSpinner();
        CBRush = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        SRushPrice = new javax.swing.JSpinner();
        BRegister = new javax.swing.JButton();
        BCancel = new javax.swing.JButton();

        jLabel1.setText("Nombre:");

        jLabel2.setText("Precio:");

        jLabel3.setText("$");

        SPrice.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), Double.valueOf(0.0d), null, Double.valueOf(1.0d)));

        CBRush.setText("Rush");
        CBRush.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBRushActionPerformed(evt);
            }
        });

        jLabel4.setText("Precio Rush:");

        jLabel5.setText("$");

        SRushPrice.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), Double.valueOf(0.0d), null, Double.valueOf(1.0d)));
        SRushPrice.setEnabled(false);

        BRegister.setText("Registrar Análisis");
        BRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BRegisterActionPerformed(evt);
            }
        });

        BCancel.setText("Cancelar");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TFName)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(CBRush)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SRushPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BRegister)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BCancel)))
                        .addGap(0, 180, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(SPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(CBRush)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(SRushPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BRegister)
                    .addComponent(BCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void CBRushActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBRushActionPerformed
        boolean enable = false;
        if (CBRush.isSelected()) {
            enable = true;
        } else {
            SRushPrice.setValue(0d);
        }
        SRushPrice.setEnabled(enable);
    }//GEN-LAST:event_CBRushActionPerformed

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BCancelActionPerformed

    private void BRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BRegisterActionPerformed
        CustomVerifier currentVerifier;
        DatabaseTest newDatabaseTest = new DatabaseTest();
        
        currentVerifier = (CustomVerifier) TFName.getInputVerifier();
        if (currentVerifier.isValid()) {
            newDatabaseTest.setName(TFName.getText());
        } else {
            return;
        }
        
        Double price = (Double) SPrice.getValue();
        newDatabaseTest.setPrice(price);
        
        boolean hasRush = CBRush.isSelected();
        newDatabaseTest.setHasRush(hasRush);
        Double rushPrice;
        if (hasRush) {
            rushPrice = (Double) SRushPrice.getValue();
            newDatabaseTest.setRushPrice(rushPrice);
            if (rushPrice == 0d) {
                int choice = JOptionPane.showConfirmDialog(getTopLevelAncestor(),
                        "El precio rush del análisis es cero. ¿Desea registrarlo?",
                        "Advertencia", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (choice == JOptionPane.NO_OPTION) {
                    return;
                }
            }
        }
        
        if (price == 0d) {
            int choice = JOptionPane.showConfirmDialog(getTopLevelAncestor(),
                                            "El precio del análisis es cero. ¿Desea registrarlo?",
                                            "Advertencia", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if(choice == JOptionPane.NO_OPTION) {
                return;
            }
        }
        
        //TODO persist
        Application.DATABASE_TEST_MANAGER.addObject(newDatabaseTest);
        JOptionPane.showMessageDialog(getTopLevelAncestor(),
                "El análisis se ha registrado satisfactoriamente.",
                "Información", JOptionPane.INFORMATION_MESSAGE);
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BRegisterActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancel;
    private javax.swing.JButton BRegister;
    private javax.swing.JCheckBox CBRush;
    private javax.swing.JSpinner SPrice;
    private javax.swing.JSpinner SRushPrice;
    private javax.swing.JTextField TFName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables
}
